<template>
  <div class="musiclike_container">
    <div class="userinfo">
      <div class="cover">
        <el-image :src="userinfo.avatarUrl" fit="cover"></el-image>
      </div>
      <div class="user_name">{{ userinfo.nickname }}的音乐库</div>
    </div>
    <div class="like_list">
      <div @click="toLikeList" class="box">
        <div class="content">
         如若暮朝懂卿年，不负如来不负卿
         闲看庭前繁华落，几缕东风无力弱
        </div>
        <div class="scr">
          <div class="left">
            <div class="info">我喜欢的音乐</div>
            <div class="count">{{ likeListId.length }}首歌</div>
          </div>
          <div class="right">
            <i @click.stop="allPlays" class="iconfont icon-bofang"></i>
          </div>
        </div>
      </div>
      <div class="list">
        <div
          @dblclick="startSong(item)"
          v-for="item in playList.slice(0, 12)"
          :key="item.id"
          class="item"
          :class="{ active_position: item.id === songId }"
        >
          <div class="left">
            <el-image
              lazy
              style="width: 35px; height: 35px; border-radius: 5px"
              :src="item.al.picUrl"
              fit="cover"
            ></el-image>
          </div>
          <div class="right">
            <div class="music_name">{{ item.name }}</div>
            <span
              @click="toSingerDetail(item1.id)"
              v-for="(item1, index1) in item.ar"
              :key="index1"
              class="singer_name"
              >{{ item1.name }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getUserPrivatePlayList,
  getLikeList,
  getMusicDetailList,
} from "@/util/userinfo.js";
export default {
  name: "Musiclike",
  data() {
    return {
      likeInfo: {},
      likeListId: [],
      playList: sessionStorage.getItem("userLikeList")
        ? JSON.parse(sessionStorage.getItem("userLikeList"))
        : [],
    };
  },
  computed: {
    ...mapGetters("userInfo", [
      //个人信息
      "userinfo",
      //登录状态
      "loginStatus",
      //用户id
      "userId",
    ]),
    ...mapGetters("musicPlay", [
      //当前播放歌曲id
      "songId",
      //是否在播放
      "isPlaying",
    ]),
  },
  created() {
    this.getUserPrivatePlayList();
  },

  methods: {
    //获取私人歌单
    async getUserPrivatePlayList() {
      let { data } = await getUserPrivatePlayList(
        this.$route.params.id ? this.$route.params.id : this.userId
      );
      // console.log(data);
      //保存私人歌单
      this.$store.dispatch("userInfo/savePrivatePlayList", data.playlist);
      sessionStorage.setItem(
        "userPrivatePlayList",
        JSON.stringify(data.playlist)
      );
      this.likeInfo = data.playlist[0];
      // console.log(this.likeInfo);
      //获取喜欢音乐列表
      this.getLikeList(data.playlist[0].id);
    },
    //获取歌单信息
    async getLikeList(id) {
      let { data } = await getLikeList(id);
      this.likeListId = data.privileges;
      // console.log(data);
      this.likeListId = data.privileges.map((v) => v.id);
      if (sessionStorage.getItem("userLikeList")) {
        return;
      }
      this.getMusicDetailList(this.likeListId.join(","));
    },
    //获取歌单音乐列表
    async getMusicDetailList(ids) {
      let { data } = await getMusicDetailList(ids);
      // console.log(data);
      this.playList = data.songs;
      sessionStorage.setItem("userLikeList", JSON.stringify(this.playList));
    },
    //播放歌曲
    async startSong(musicDetail) {
      //保存当前播放列表
      this.$store.dispatch("musicPlay/savePlayList", this.playList);
      sessionStorage.setItem("playList", JSON.stringify(this.playList));

      //保存播放状态
      this.$store.dispatch("musicPlay/saveIsPlaying", "true");

      //保存当前歌曲id
      this.$store.dispatch("musicPlay/saveSongId", musicDetail.id);
      sessionStorage.setItem("songId", musicDetail.id);
    },
    //跳转到歌手详情
    toSingerDetail(id) {
      this.$router.push("/singerInfo/" + id);
    },
    //跳转喜欢列表
    toLikeList() {
      this.$router.push("/userLove/" + this.userId);
    },
    //全部播放
    allPlays() {
      //保存当前播放列表
      this.$store.dispatch("musicPlay/savePlayList", this.playList);
      sessionStorage.setItem("playList", JSON.stringify(this.playList));

      //保存播放状态
      this.$store.dispatch("musicPlay/saveIsPlaying", "true");

      //保存当前歌曲id
      this.$store.dispatch("musicPlay/saveSongId", this.playList[0].id);
      sessionStorage.setItem("songId", this.playList[0].id);
    },
  },
};
</script>

<style lang="less" scoped>
.musiclike_container {
  .userinfo {
    display: flex;
    align-items: center;
    padding: 20px 0;
    .cover {
      width: 45px;
      height: 45px;
      .el-image {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .user_name {
      margin-left: 20px;
      font-size: 40px;
      font-weight: 600;
    }
  }
  .like_list {
    padding: 10px 0;
    display: flex;
    .box {
      width: 23vw;
      height: 190px;
      background-color: #eaeffd;
      border-radius: 15px;
      padding: 20px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .content {
        font-size: 20px;
        font-weight: 600;
        color: #335eea;
      }
      .scr {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left {
          .info {
            font-size: 22px;
            font-weight: 600;
            color: #335eea;
          }
          .count {
            font-size: 16px;
            color: #335eea;
          }
        }
        .right {
          background-color: #335eea;
          padding: 7px;
          border-radius: 50%;
          transition: all 0.5s;
          &:hover {
            transform: scale(1.1);
          }
          &:active {
            transform: scale(0.8);
          }
          i {
            font-size: 15px;
            color: #fff;
            background-color: #335eea;
            z-index: 555;
          }
        }
      }
    }
    .list {
      flex: 1;
      //   background-color: #ddd;
      margin-left: 30px;
      //   padding: 7px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      align-items: center;
      .item {
        cursor: pointer;
        display: flex;
        align-items: center;
        width: 30%;
        background-color: #fff;
        padding: 5px;
        border-radius: 10px;
        &:hover {
          background-color: #f2f2f4;
        }

        .left {
        }
        .right {
          margin-left: 13px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          .music_name {
            font-size: 16px;
            font-weight: 600;
          }
          .singer_name {
            color: rgb(100, 89, 89);
            font-size: 12px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            &:hover {
              text-decoration: underline;
            }
          }
        }
      }
      .active_position {
        color: #335eea;
        background: #eaeffd;
        .info {
          .name,
          .creator_name {
            color: #335eea;
          }
        }
        .album,
        .timer {
          color: #335eea;
        }
      }
    }
  }
}
</style>